package com.bos.controller.json;

import com.bos.dao.InstansiDAO;
import com.bos.dao.KabupatenDAO;
import com.bos.domain.Instansi;
import com.bos.domain.Kabupaten;
import com.bos.editor.InstansiEditor;
import com.bos.editor.KabupatenEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/18/11
 * Time: 3:06 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class InstansiJsonController {

    private InstansiDAO instansiDAO;
    private KabupatenDAO kabupatenDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setInstansiDAO(InstansiDAO instansiDAO) {
        this.instansiDAO = instansiDAO;
    }

    public KabupatenDAO getKabupatenDAO() {
        return kabupatenDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/instansi.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Instansi instansi) {
        if (instansi == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            instansiDAO.save(instansi);
            result.put("success", true);
            result.put("instansi", instansi);
            result.put("message", "Sukses Save Instansi " + instansi);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("instansi", instansi);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Instansi " + instansi);
        }
        return result;
    }

    @RequestMapping(value = "/instansi/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Instansi instansi) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            instansiDAO.delete(instansi);
            result.put("success", true);
            result.put("message", "Sukses Delete Instansi " + instansi);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Instansi " + instansi);
        }
        return result;
    }

    @RequestMapping(value = "/instansi.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Instansi instansi) {
        if (instansi == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            instansiDAO.merge(instansi);
            result.put("success", true);
            result.put("instansi", instansi);
            result.put("message", "Sukses Update Instansi " + instansi);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("instansi", instansi);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Update Instansi " + instansi);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(Instansi.class, new InstansiEditor(instansiDAO));
    }

}
